<?php
	class BannerModel extends Model {
	public function display( $start=0 , $perpage=20 , $searchString="" , $searchColumns = array("title","content") , $sortColumn="id,created,modified" , $sortCriteria="ASC" ){
			
			$start = is_numeric($start)&&$start>-1?$start:0;
			$perpage = is_numeric($perpage)&&$perpage>-1?$perpage:20;
			
			$result = array();
			$query = "SELECT `id`,`title`,`content`,`published`,`level`, `style` , `index` , `created`, `modified`
					FROM banners " ;
			if ( $searchString != "" ){
				$searchColumns = ( $searchColumns===false || ( is_array($searchColumns) && empty($searchColumns) ) ) ? array("title_kh","title_en","fulltext_kh","fulltext_en") : $searchColumns ; 
				if ( is_array($searchColumns) && !empty($searchColumns) ){
				$conditions = "" ;
					$conditions = array() ;
					foreach( $searchColumns AS $columnName ){
						$conditions[] = " $columnName LIKE '%$searchString%' " ;
					}
					$conditions = " WHERE " . implode("OR", $conditions );
					$sortColumn = $sortColumn===""||$sortColumn===false?"id,created,modified":$sortColumn;
					$sortCriteria = $sortCriteria===""||$sortCriteria===false?"DESC":$sortCriteria;
					$query .= "$conditions " ;
				}
			}
			if( $sortColumn != "" || $sortColumn != false ){
				$sortCriteria = $sortCriteria!=""&&$sortCriteria!=false?$sortCriteria:"DESC";
				$query .= " ORDER BY $sortColumn $sortCriteria " ;
			}
			$this->queryGetRow = $query ;
			$query .= " LIMIT $start , ".$perpage."; " ;
			//echo $query;
			$result = $this->executeQueryAsObject( $query ) ;
			return $result;
		}
	public function displayfront($position=0){
		$query = "SELECT * FROM banners WHERE published=1 AND level='$position' ORDER BY banners.index ASC ;" ;
		return $this->executeQueryAsObject($query);
	}
	public function add( $title , $content , $publish , $level = 1 , $style = 0 ){
		$content = $this->sqlRealEscape($content);
        $title = $this->sqlRealEscape($title);
		$indexes = $this->reindexBanner($level);
		$index = $indexes[count($indexes)-1] + 1 ;
		$query = "INSERT INTO banners SET title='".$title."' , content='".$content."' , published='$publish' , level='$level' , style = '$style', banners.index='$index' , created=NOW() , modified=NOW() " ;
		$this->executeQuery( $query );
	}
		public function edit( $id , $title , $content , $publish , $level = 1 , $style = 0 , $index = 1 ){
			$index++;
            $title = $this->sqlRealEscape($title); 
			$content = $this->sqlRealEscape($content);
			$query = "SELECT * FROM banners WHERE id='$id' ; ";
			$banner = $this->executeQueryAsObject( $query );
			if ( count($banner)>0 ){
				$banner=$banner[0];
				print_r( $banner );
				$query = "UPDATE banners SET banners.index='$banner->index' WHERE level='$level' AND banners.index='$index' ;";
				$this->executeQuery( $query );
			}
			$query = "UPDATE banners SET title='$title' , content='$content' , published='$publish' , level='$level' , style = '$style' , banners.index='$index' , modified=NOW() WHERE id='$id' " ;
			$this->executeQuery( $query );
		}
		public function delete( $id ){
			$query = "DELETE FROM banners WHERE id='$id' ;" ;
			$this->executeQuery( $query );
		}
		public function find( $id ){
			$query = "SELECT * FROM banners WHERE id='$id' ";
			return $this->executeQueryAsObject( $query );
		}
		public function getTotalRecords(){
			return $this->getRowWithQuery($this->queryGetRow) ;
		}
		public function published($id,$publish){
			$query = "UPDATE banners SET published=$publish WHERE id = $id ;";
			$this->executeQuery($query);
		}
		public function topBanners(){
			return $this->getBannersLevel(0);
		}
		public function bottomBanners(){
			return $this->getBannersLevel(2);
		}
		public function rightBanners(){
			return $this->getBannersLevel(1);
		}
		public function getBannerIndex($position=0){
			$query = "SELECT * FROM banners WHERE level = $position ORDER BY banners.index ASC;" ;
			$banners = $this->executeQueryAsObject($query) ;
			$indexes = array();
			foreach( $banners as $banner ){
				$indexes[] = $banner->index;
			}
			return $indexes;
		}
		public function reindexBanner($position=0){
			$query = "SELECT * FROM banners WHERE level = '$position' ORDER BY banners.index ASC ;";
			$banners = $this->executeQueryAsObject($query);
			$index = 1 ;
			$indexes = array();
			foreach( $banners AS $banner ){
				$this->executeQuery("UPDATE banners SET banners.index = '$index' WHERE level = '$position' AND id ='$banner->id' ;");
				$indexes[] = $index;
				$index++;
			}
			return $indexes;
		}
		public function reindexWithId($position,$id,$set_index){
			$query = "SELECT * FROM banners WHERE level = '$position' ORDER BY banners.index ASC ;";
			$banners = $this->executeQueryAsObject($query);
			$index = 1 ;
			$indexes = array();
			foreach( $banners AS $banner ){
				if ( $banner->index == $set_index ){
					$this->executeQuery("UPDATE banners SET banners.index = '$set_index' WHERE level = '$position' AND id ='$id' ;");
					$indexes[] = $set_index;
					$index=$set_index+1;
					$this->executeQuery("UPDATE banners SET banners.index = '$index' WHERE level = '$position' AND id ='$banner->id' ;");
					$indexes[] = $index;
				}
				else if ( $banner->index > $set_index ){
					$this->executeQuery("UPDATE banners SET index = '$index' WHERE level = '$position' AND id ='$banner->id' ;");
				}
				$indexes[] = $index;
				$index++;
			}
			return $indexes;
		}
		public function getBannersLevel($position=0){
			$position!==1&&$position!==2&&$position!==0?$position=1:false;
			$query = "SELECT * FROM banners WHERE level = $position AND published = 1 ORDER BY banners.index ASC ;" ;
			return $this->executeQueryAsObject($query);
		}
	}